# BSD3.0
# cili@163.com

import tkinter as tk
from tkinter import font
import random

def generate_captcha(canvas):
    generate_button.config(state='disable')
    canvas.delete('all')
    captcha_text = ''.join(random.choice(CHARS) for _ in range(6))
    font_obj = font.Font(family='Arial', size=36, weight='bold')
    canvas.create_text(120, 34, text=captcha_text, font=font_obj, fill='blue')
    data = [(random.randint(0, 300),random.randint(0, 60),f'#{random.randint(0, 255):02x}{random.randint(0, 255):02x}{random.randint(0, 255):02x}') for _ in range(4000)]
    [canvas.create_oval(x, y, x+1, y+1, fill=color, outline=color) for x,y,color in data]
    generate_button.config(state='normal')

CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

window = tk.Tk()
window.title("验证码生成器")

# 创建一个画布来绘制验证码
canvas = tk.Canvas(window, width=300, height=60, bg='white')
canvas.pack(padx=5)

# 生成验证码按钮
generate_button = tk.Button(window, text="生成验证码", command=lambda: generate_captcha(canvas))
generate_button.pack()

# 初始化时生成一个验证码
#generate_captcha(canvas)
window.update_idletasks()
width = window.winfo_width()
height = window.winfo_height()
x = (window.winfo_screenwidth() // 2) - (width // 2)
y = (window.winfo_screenheight() // 2) - (height // 2)
window.geometry('{}x{}+{}+{}'.format(width, height, x, y))

window.mainloop()
